package GameMain.View.WaitGame
{
	import Engine.Module.ModuleBase;
	
	import GameMain.Game;
	import GameMain.Macro.MacroEvent;
	import GameMain.Model.Player;
	import GameMain.View.PTGame.GameManager;
	
	import flash.display.Sprite;
	
	import library.SocketData.SocketData;
	
	public class WaitGame extends ModuleBase
	{
		private var _teamItemList:Array = new Array();
		private var _boolSourceLoad:Boolean = false;
		private var _mapID:int = 0;
		private var _currentPre:int = -1;
		
		public function WaitGame(p_content:Sprite=null)
		{
			super(p_content);
			this._teamItemList.push(new TeamItem(this._content["TEAM_1"]));
			this._teamItemList.push(new TeamItem(this._content["TEAM_2"]));
			this._teamItemList.push(new TeamItem(this._content["TEAM_3"]));
			this._teamItemList.push(new TeamItem(this._content["TEAM_4"]));
			this._teamItemList.push(new TeamItem(this._content["TEAM_5"]));
			this._teamItemList.push(new TeamItem(this._content["TEAM_6"]));
			this._teamItemList.push(new TeamItem(this._content["TEAM_7"]));
			this._teamItemList.push(new TeamItem(this._content["TEAM_8"]));
		}
		
		public function onRegesterWaitGame():void
		{
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_GAME_SUCCESS,onSetMapID);
		}
		
		/*游戏匹配成功调用*/
		private function onSetMapID(data:SocketData):void
		{
			this._mapID = data.readShort();
			this.resetItem();
			GameManager.PVP_Game.regesterEvent();
			Game.ModuleMgr.showGameWait();
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_GAME_SUCCESS);
		}
		
		public function onLoadIngGame():void
		{
			if(GameManager.BooleanLoad == false)
			{
				GameManager.setCallBackFun(onLoadSource);
				GameManager.LoadProfessionModel();
			}
			else
			{
				this.onLoadSourceOver();
			}
		}
		
		public function setTeamItem(player:Player):void
		{
			var item:TeamItem = this.getItem(player.playerItemID);
			item.setTeamState(true);
			item.setPlayerName(player.playerName);
		}
		
		private function getItem(playerItemID:int):TeamItem
		{
			var index:int = int(playerItemID / 10 - 1)*4 + (playerItemID % 10) - 1;
			return this._teamItemList[index] as TeamItem;
		}
		
		private function onLoadSource(per:int):void
		{
			if(this._currentPre == per)
				return;
			this._currentPre = per;
			if( per != 100 )
			{
				var data:SocketData = new SocketData;
				data.writeInt8(per);
				this.sendMsg(MacroEvent.MSG_TO_SERVER_LOAD_PER,data);
			}
			else
				this.onLoadSourceOver();
		}
		
		private function onLoadSourceOver():void
		{
			GameManager.PVP_Game.initGameFromData(this._mapID);
			GameManager.PVP_Game.setNewPlayer(false);
			var data:SocketData = new SocketData;
			data.writeInt8(100);
			this.sendMsg(MacroEvent.MSG_TO_SERVER_LOAD_PER,data);
		}
		
		private function onEnterGame(data:SocketData):void
		{
			Game.ModuleMgr.showPVPGame();
		}
		
		private function playerLoadPer(data:SocketData):void
		{
			var playerItemID:int = data.readInt8();
			this.getItem(playerItemID).setPercent(data.readInt8());
		}
		
		private function resetItem():void
		{
			for(var i:int = 0;i<8;i++)
			{
				this._teamItemList[i].setTeamState(false);
			}
		}
		
		
		override protected function onShowBack():void
		{
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_ENTER_GAME,onEnterGame);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_LOAD_PER,playerLoadPer);
		}
		
		override protected function onDisposeBack():void
		{
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_ENTER_GAME);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_LOAD_PER);
		}
		
	}
}